package com.drawat.listviewtest;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {

    private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1;

    private static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;

    private NotesDbAdapter _dbHelper;
    private Cursor _notesCursor;
	private ArrayList<String> _menuItems;

	public MainActivity() {
		super();

		_menuItems = new ArrayList<String>();
		_menuItems.add("View");
		_menuItems.add("Edit");
		_menuItems.add("Remove");
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);

		ExpandListAdapter expandableAdapter = new ExpandListAdapter(MainActivity.this, this.setStandardGroups());

		ExpandableListView expandableListView = ((ExpandableListView) this.findViewById(R.id.expandable_list));
		expandableListView.setAdapter(expandableAdapter);
        
		this.registerForContextMenu(expandableListView);
	}

    private void fillData() {
        // Get all of the rows from the database and create the item list
        _notesCursor = _dbHelper.fetchAllEvents();
        startManagingCursor(_notesCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{NotesDbAdapter.KEY_TITLE};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.notes_row, _notesCursor, from, to);
        setListAdapter(notes);
    }

	
	public ArrayList<ExpandableListGroup> setStandardGroups() {
		_dbHelper = new NotesDbAdapter(this);
        _dbHelper.open();

        _notesCursor = _dbHelper.fetchAllEvents();
		
		ArrayList<ExpandableListGroup> list = new ArrayList<ExpandableListGroup>();

		ArrayList<Event> events1 = new ArrayList<Event>(); //  academics
		ArrayList<Event> events2 = new ArrayList<Event>(); // sprots
		ArrayList<Event> events3 = new ArrayList<Event>(); // voluenteering
		ArrayList<Event> events4 = new ArrayList<Event>(); // party
		ArrayList<Event> events5 = new ArrayList<Event>(); // free food
		
		if(_notesCursor!=null) {
			if(_notesCursor.moveToFirst()) {
				do {
					if(_notesCursor.getString(_notesCursor.getColumnIndex("categories")).equals("Academics")){		
						// TODO add to academics
					} else if(_notesCursor.getString(_notesCursor.getColumnIndex("categories")).equals("Sports")) {
						// TODO add to sports
					}else if(_notesCursor.getString(_notesCursor.getColumnIndex("categories")).equals("Volunteering")) {
						// TODO add to volunteering
					}else if(_notesCursor.getString(_notesCursor.getColumnIndex("categories")).equals("Party")) {
						// TODO add to party
					}else if(_notesCursor.getString(_notesCursor.getColumnIndex("categories")).equals("Free Food")) {
						// TODO add to free food
					} else {
						Log.d("ERROR","Categorie not found");
					}					
				} while(_notesCursor.moveToNext());
			}
		}
		
		
//		events.add(new Event("Dhruv's Birthday Party", "Feb 17, 2012")); 
//		events.add(new Event("Frat Party at Alpha Beta Gamma", "August 26, 2012"));
//		events.add(new Event("Free Food at CS Dinner", "July 27, 2012"));
//
//		list.add(new ExpandableListGroup("Attending", events));
//
//		events = new ArrayList<Event>();
//		events.add(new Event("Dhruv's Birthday Party", "Feb 17, 2012")); 
//		events.add(new Event("Frat Party at Alpha Beta Gamma", "August 26, 2012"));
//		events.add(new Event("Free Food at CS Dinner", "July 27, 2012"));
//
//		list.add(new ExpandableListGroup("Academic", events));
//
//		events = new ArrayList<Event>();
//		events.add(new Event("Dhruv's Birthday Party", "Feb 17, 2012")); 
//		events.add(new Event("Frat Party at Alpha Beta Gamma", "August 26, 2012"));
//		events.add(new Event("Free Food at CS Dinner", "July 27, 2012"));
//
//		list.add(new ExpandableListGroup("Free Food", events));
//
//		events = new ArrayList<Event>();
//		events.add(new Event("Dhruv's Birthday Party", "Feb 17, 2012")); 
//		events.add(new Event("Frat Party at Alpha Beta Gamma", "August 26, 2012"));
//		events.add(new Event("Free Food at CS Dinner", "July 27, 2012"));
//
//		list.add(new ExpandableListGroup("Community Service", events));
//
//		events = new ArrayList<Event>();
//		events.add(new Event("Dhruv's Birthday Party", "Feb 17, 2012")); 
//		events.add(new Event("Frat Party at Alpha Beta Gamma", "August 26, 2012"));
//		events.add(new Event("Free Food at CS Dinner", "July 27, 2012"));
//
//		list.add(new ExpandableListGroup("Sports", events));
//
		return list;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		this.getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
		if (view.getId()==R.id.expandable_list) {
			for(String item: _menuItems) {
				menu.add(item);
			}
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		String menuItemName = _menuItems.get(item.getItemId());

		// Process selection here

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.add_event:
			Log.d("MainActivity", "Add Event button was clicked!");
//			Intent intent = new Intent(this, Notepadv2.class);
//			this.startActivity(intent);
			Cursor c = new NotesDbAdapter(this).fetchAllEvents();
	        Intent i = new Intent(this, NoteEdit.class);
	        i.putExtra(NotesDbAdapter.KEY_ROWID, item.getItemId());
	        i.putExtra(NotesDbAdapter.KEY_TITLE, c.getString(
	                c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
	        i.putExtra(NotesDbAdapter.KEY_DATE, c.getString(
	                c.getColumnIndexOrThrow(NotesDbAdapter.KEY_DATE))); 
	        i.putExtra(NotesDbAdapter.KEY_TIME_START, c.getString(
	                c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TIME_START)));
	        i.putExtra(NotesDbAdapter.KEY_TIME_END, c.getString(
	                c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TIME_END)));
	        i.putExtra(NotesDbAdapter.KEY_LOCATION, c.getString(
	                c.getColumnIndexOrThrow(NotesDbAdapter.KEY_LOCATION)));
	        i.putExtra(NotesDbAdapter.KEY_DESCRIPTION, c.getString(
	                c.getColumnIndexOrThrow(NotesDbAdapter.KEY_DESCRIPTION)));
	        
	        startActivityForResult(i, ACTIVITY_EDIT);
			
			return true;
		case R.id.refresh_events:
			Log.d("MainActivity", "Refresh button was clicked!");
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}